/*
 *  MTouchware_senders.h
 *
 *  Header of MTouchware_senders.c that handles sending the messages
 *  to other applications.
 *
 *  Copyright (C) 2010 LIMSI-CNRS (Orsay-France) and EFREI (Paris-France)
 *
 *  Authors: Rami Ajaj, Nicolas Flasque and Frederic Vernier.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  $Id$
 */

#ifndef MTOUCHWARE_SENDERS_H
#define MTOUCHWARE_SENDERS_H


#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifndef WIN32
//  #include <sys/time.h>
  #include <unistd.h>
#else 
//  #include <time.h>
#endif

#include "lo/lo.h"



/**
 * \defgroup MTouchware senders
 *
 * Handlers sending the messages to other applications messages
 * @{
 */

int _counter;
extern lo_address *MTouchware_senders;
extern int nb_MTouchware_senders;

/**
 * \brief Broadcasts the message on all senders in the senders list
 *
 * \param path char* specifying the path of the message.
 * \param types char* specifying types of arguments this message handles.
 *
 * Note: it is done in a Variadic Macro because of lo_send variadic function
 */
#define send_message(path, types, ...) for (_counter=0; _counter<nb_MTouchware_senders; _counter++) {\
		if (lo_send(MTouchware_senders[_counter], path, types, __VA_ARGS__ ) == -1) {\
			printf("OSC error %d: %s\n", lo_address_errno(MTouchware_senders[_counter]), lo_address_errstr(MTouchware_senders[_counter]));}}

/**
 * \brief Creates senders list with one sender
 *
 * \param adress_out char* specifying the adress of the first sender where messages will be sent.
 * \param port_out char* specifying the port of the first sender on which messages will be sent.
 *
 */
void create_MTouchware_senders(char* adress_out, char* port_out);

/**
 * \brief Deletes the senders list
 *
 */
void delete_MTouchware_senders();

/**
 * \brief Adds a new sender to senders list
 *
 * \param adress_out char* specifying the adress of the new sender where messages will be sent.
 * \param port_out char* specifying the port of the new sender on which messages will be sent.
 *
 */
int add_MTouchware_sender(char* adress_out, char* port_out);

/**
 * \brief calls send_message with the parropriate set of arguments.
 * 
 * \param path char* specifying the path of the message.
 * \param types char* specifying types of arguments this message handles.
 * \param argv loargs** specifying the arguments to be sent.
 *
 * Note: It is called when the number of arguments is unknown (e.g. TUIO alive message)
 *
 */
void send_message_lo_args(const char *path, const char *types, lo_arg **argv);


/** @} */
#ifdef __cplusplus
}
#endif

#endif
